package com.marsiot.sdk;

import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;

import com.marsiot.agent.Agent;
import com.marsiot.api.ApiCommand;
import com.marsiot.sdk.MyConfig;

/**
 * @author xiangstudio
 */

public class Api {

    private static Agent agent = new Agent();

    public static void main(String[] args) {

        if (args.length <= 0) {
            System.out.print("\nCommand help:\n");
            System.out.print("apiSendMessage <type> <message> \n");
            System.out.print("apiOcrIdcard <filename> \n");
            System.exit(-1);
        }

        Object[] paras = new Object[args.length-1];
        for (int i=1,j=0; i<args.length; i++,j++) {
            paras[j] = args[i];
        }

        Class<?>[] types = new Class[paras.length];
        for (int j=0; j<paras.length; j++) {
            types[j] = paras[j].getClass(); 
        }

        ApiCommand apiCommand = new ApiCommand();	
	apiCommand.setSiteToken(MyConfig.mySiteToken);
	apiCommand.setModelName(MyConfig.myModelName);
	apiCommand.setModelDescription(MyConfig.myModelDescription);
        try {
            Method method = apiCommand.getClass().getMethod(args[0], types); 
            String jsonResult = (String)method.invoke(apiCommand, paras);
            System.out.print(jsonResult);
        } catch (NoSuchMethodException e) {
            System.out.print(args[0]+": No such method or parameters mismatch!\n");
            System.exit(-1);
        } catch (IllegalAccessException e) {
            System.out.print("Ex: Illegal access!\n");
            System.exit(-1);
        } catch (IllegalArgumentException e) {
            System.out.print("Ex: Illegal argument!\n");
            System.exit(-1);
        } catch (InvocationTargetException e) {
            System.out.print("Ex: Invocation target!\n");
            System.exit(-1);
        }

    }

}


